package com.googlecode.hyakutaded;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.hyakutaded.json.JSONException;
import com.googlecode.hyakutaded.json.JSONObject;
import com.googlecode.hyakutaded.pubsub.Topic;
import com.googlecode.hyakutaded.pubsub.Topics;

public class SendServlet extends HttpServlet {
	private String key = null;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		key = config.getInitParameter("key");
	}

	private static final long serialVersionUID = 1L;
	private Clients clients = Clients.getInstance();
	private Topics topics = Topics.getInstance();

	public void doTopicSend(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException {
		String topicId = req.getParameter("topicId");
		String action = req.getParameter("action").substring("topic".length());
		try {
			if (topicId != null) {
				Topic topic = topics.getTopic(topicId);
				if ("Subscribe".equals(action)) {
					String clientId = req.getParameter("clientId");
					Client client = clients.getClient(clientId);
					topic.addClient(client);
					return;
				}
				if ("Unsubscribe".equals(action)) {
					String clientId = req.getParameter("clientId");
					Client client = clients.getClient(clientId);
					topic.removeClient(client);
					return;
				}
				String valueString = req.getParameter("value");
				JSONObject value;
				value = new JSONObject(valueString);
				if ("Add".equals(action)) {
					topic.add(value);
					return;
				}
				if ("Set".equals(action)) {
					topic.set(value);
					return;
				}
				if ("Update".equals(action)) {
					String subkey = req.getParameter("subkey");
					if (subkey != null) {
						topic.update(subkey, value);
					}
					return;
				}
			}
		} catch (JSONException e) {
			throw new ServletException(e.getMessage(), e);
		}
	}

	public void doClientSend(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException {
		String clientId = req.getParameter("clientId");
		String name = req.getParameter("name");
		String valueString = req.getParameter("value");
		String action = req.getParameter("action").substring("client".length());
		JSONObject value;
		try {
			value = new JSONObject(valueString);
			if (clientId != null) {
				Client c = clients.getClient(clientId);
				if ((name != null) && (value != null)) {
					if ("Add".equals(action)) {
						c.add(name, value);
						return;
					}
					if ("Set".equals(action)) {
						c.set(name, value);
						return;
					}
					if ("Update".equals(action)) {
						String subkey = req.getParameter("subkey");
						if (subkey != null) {
							c.update(name, subkey, value);
						}
						return;
					}
				}
			}
		} catch (JSONException e) {
			throw new ServletException(e.getMessage(), e);
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String k = req.getParameter("key");
		String action = req.getParameter("action");
		if (k != null) {
			if (k.equals(key)) {
				if (action.startsWith("client")) {
					doClientSend(req, resp);
				}
				if (action.startsWith("topic")) {
					doTopicSend(req, resp);
				}
			}
		}
	}
}
